package com.cyj.springbootservice.controller.authentication;

import com.cyj.springbootservice.api.entity.AccessToken;
import com.cyj.springbootservice.api.entity.SignTicket;
import com.cyj.springbootservice.api.entity.Ticket;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.List;

/**
 * @description:
 * @Author C_Y_J
 * @create 2021/10/9 11:00
 **/
@Component
public class AuthenticationBuilder {

    private final static String getAccessTokenUrl = "https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token";
    private final static String wbappid = "IDAwrYz6";
    private final static String secret = "5RRvFajVfolJXwhqz7b7AKvo8QoEUqJXV57mDuztwJcDD2wvH4zsYWXUpshIEPMr";
    private final static String grant_type = "client_credential";
    private final static String version = "1.0.0";

    private final static String getSignTicketUrl = "https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket";
    private final static String type = "SIGN";

    private final static String getSignatureUrl = "https://miniprogram-kyc.tencentcloudapi.com/api/server/getfaceid";
    private final static String userId = "1001";

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 获取 Access Token
     */
    public String getAccessToken() {
        StringBuilder sb = new StringBuilder();
        String URL =
                sb.append(getAccessTokenUrl)
                        .append("?").append("app_id=").append(wbappid)
                        .append("&").append("secret=").append(secret)
                        .append("&").append("grant_type=").append(grant_type)
                        .append("&").append("version=").append(version).toString();
        System.out.println(URL);
        AccessToken accessToken = restTemplate.getForEntity(URL, AccessToken.class).getBody();
        System.out.println(accessToken);
        System.out.println("\n");
        System.out.println("\n");
        return accessToken.getAccess_token();

    }

    /**
     * 获取 SIGN Ticket
     * SIGN 类型的 Ticket，其有效期为60分钟，且可多次使用。
     */
    public String getSignTicket(String access_token) {
        StringBuilder sb = new StringBuilder();
        String URL =
                sb.append(getSignTicketUrl)
                        .append("?").append("app_id=").append(wbappid)
                        .append("&").append("access_token=").append(access_token)
                        .append("&").append("type=").append(type)
                        .append("&").append("version=").append(version).toString();
        System.out.println(URL);
        SignTicket signTicket = restTemplate.getForEntity(URL, SignTicket.class).getBody();
        Ticket ticket = signTicket.getTickets().get(0);
        System.out.println(ticket);
        System.out.println("\n");
        System.out.println("\n");
        return ticket.getValue();
    }

    /**
     * [1.0.0, IDAXXXXX, XO99Qfxlti9iTVgHAjwvJdAZKN3nMuUhrsPdPlPVKlcyS50N6tlLnfuFBPIucaMS, kHoSxvLZGxSoFsjxlbzEoUzh5PAnTU7T, userID19959248596551]
     * <p>
     * 1.0.0  IDAXXXXX  XO99Qfxlti9iTVgHAjwvJdAZKN3nMuUhrsPdPlPVKlcyS50N6tlLnfuFBPIucaMS  kHoSxvLZGxSoFsjxlbzEoUzh5PAnTU7T  userID19959248596551
     *
     * @param values
     * @return
     */
    public static String sign(List<String> values) {
        if (values == null) {
            throw new NullPointerException("values is null");
        }
        values.removeAll(Collections.singleton(null));// remove null
        java.util.Collections.sort(values);
        StringBuilder sb = new StringBuilder();
        for (String s : values) {
            sb.append(s);
        }
        return Hashing.sha1().hashString(sb, Charsets.UTF_8).toString().toUpperCase();
    }
}
